/**
 * file
 */
package com.tdkj.action.front.member.buyer;

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.shop.BeanOrder;
import com.tdkj.model.shop.BeanOrderItem;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.shop.IBeanOrderService;
import com.tdkj.util.PublicConfing;

public class GoodOrderAction extends BaseAction {

	private Long orderId;
	private List<BeanOrderItem> items;
	private String state;
	
	private IBeanOrderService orderService;
	
	public String execute(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		String name = this.getQueryInfo().get("name");
		String hql = "select bean from " + BeanOrder.class.getName() + " as bean, " + BeanOrderItem.class.getName() + " as item  where item.order.id=bean.id and bean.buyer.id='"+userId+"' ";
		if(state!=null && !"".equals(state)){
			hql = hql + " and bean.state='"+state+"' ";
		}
		if(name!=null && !"".equals(name)){
			hql = hql + " and item.good.name like '%"+name+"%' ";
		}
		hql = hql + " order by bean.createTime desc ";
		Query query = this.createQuery(hql);
		this.queryByPage(query, "goodOrderAction.action", 10);
		return "success";
	}
	
	public String orderItems(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		
		String name = this.getQueryInfo().get("name");
		String hql = " select bean from " + BeanOrderItem.class.getName() + " as bean where bean.order.buyer.id='"+userId+"' " ;
		if(name!=null && !"".equals(name)){
			hql = hql + " bean.good.name like '%"+name+"%'";
		}
		hql = hql + " order by createTime desc ";
		Query query = this.createQuery(hql);
		this.queryByPage(query, "goodOrderAction!orderItems.action", 10);
		return "orderItems";
	}
	
	public String orderPay(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		orderService.createPay(userId, orderId);
		
		return execute();
	}
	
	public String orderDelivery(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		orderService.createDelivery(userId, orderId);
		
		return execute();
	}
	
	public String orderReceive(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		orderService.createReceive(userId, orderId);
		
		return execute();
	}

	public String orderCancel(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		orderService.createCancel(userId, orderId);
		
		return execute();
	}
	 
	public List<BeanOrderItem> getItems() {
		return items;
	}

	public void setItems(List<BeanOrderItem> items) {
		this.items = items;
	}

	@Resource
	public void setOrderService(IBeanOrderService orderService) {
		this.orderService = orderService;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public Long getOrderId() {
		return orderId;
	}

	public void setOrderId(Long orderId) {
		this.orderId = orderId;
	}
	
	
}
